//
//  SearchVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/18.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "SearchVC.h"
#import "CityBtn.h"
#import "UITextField+Placeholder.h"
#import "SearchResultVC.h"
#import "CityTableView.h"

@interface SearchVC () <UITextFieldDelegate, CityChooseCallBack, UIGestureRecognizerDelegate>

// 头部城市按钮
@property (nonatomic, strong) CityBtn *cityBtn;
// 搜索
@property (nonatomic, strong) UIView *searchView;
// 文本框
@property (nonatomic, strong) UITextField *search_tf;
// 所搜按钮
@property (nonatomic, strong) UIButton *searchBtn;
// 取消按钮
@property (nonatomic, strong) UIButton *cancelBtn;

// 城市展示图
@property (nonatomic, strong) UIView *cityBaseV;
@property (nonatomic, strong) CityTableView *cityTable;

@property (nonatomic, strong) NSArray *city_data;

@end

@implementation SearchVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = UICOLOR_HEX(0xffffffff);
    
    [self setHeadView];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.navigationController.navigationBar.alpha = 0;
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
}

#pragma mark - 头部视图
- (void)setHeadView {
    
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GET_SCREEN_WIDTH(), SafeAreaTopHeight)];
    headV.backgroundColor = Base_color;
    [self.view addSubview:headV];
    
    CGFloat cityWidth = [_cityName length]*16;
    
    _cityBtn = [[CityBtn alloc] initWithFrame:CGRectMake(10, SafeAreaTopHeight-7-30, 8+15+5+cityWidth, 30)];
    [_cityBtn setImage:[UIImage imageNamed:@"home_city"] forState:UIControlStateNormal];
    [_cityBtn setTitle:_cityName forState:UIControlStateNormal];
    [_cityBtn setTitleColor:UICOLOR_HEX(0xffffffff) forState:UIControlStateNormal];
    _cityBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_cityBtn addTarget:self action:@selector(showAllCity) forControlEvents:UIControlEventTouchUpInside];
    [headV addSubview:_cityBtn];
    
    _cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(GET_SCREEN_WIDTH()-44, SafeAreaTopHeight-7-30, 44, 30)];
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:UICOLOR_HEX(0xffffffff) forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_cancelBtn addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside];
    [headV addSubview:_cancelBtn];
    
    _searchView = [[UIView alloc] initWithFrame:CGRectMake(_cityBtn.right, SafeAreaTopHeight-7-30, GET_SCREEN_WIDTH()-_cancelBtn.width-_cityBtn.right, 30)];
    _searchView.backgroundColor = UICOLOR_HEX(0xffffffff);
    _searchView.layer.cornerRadius = 5;
    _searchView.layer.borderColor = UICOLOR_HEX(0xffe0e0e0).CGColor;
    _searchView.layer.borderWidth = 1.0f;
    [headV addSubview:_searchView];
    
    
    _search_tf = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, _searchView.width-20-16-7-7, _searchView.height)];
    _search_tf.placeholder = @"请输入关键字搜索";
    [_search_tf setPlaceHolderFont:[UIFont systemFontOfSize:15]];
    [_search_tf setPlaceHolderColor:UICOLOR_HEX(0xff666666)];
    _search_tf.textColor = UICOLOR_HEX(0xff000000);
    _search_tf.delegate = self;
    _search_tf.returnKeyType = UIReturnKeySearch;
    [_searchView addSubview:_search_tf];
    
    [_search_tf becomeFirstResponder];
    
    _searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(_searchView.width-16-7-7, 0, 16+7+7, _searchView.height)];
    [_searchBtn setImage:[UIImage imageNamed:@"home_search"] forState:UIControlStateNormal];
    [_searchBtn addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    [_searchView addSubview:_searchBtn];
    
    UIImageView *searchImage = [[UIImageView alloc] initWithFrame:CGRectMake(_searchBtn.width-16-7, (_searchBtn.height-15)/2.0f, 16, 15)];
    searchImage.image = [UIImage imageNamed:@"home_search"];
    [_searchBtn addSubview:searchImage];
}

#pragma mark - 搜索
- (void)search {
    
    if ([ProjectTools isValiString:_search_tf.text]) {
        
        [SVProgressHUD showInfoWithStatus:@"请输入关键字搜索"];
        [SVProgressHUD dismissWithDelay:1.5f];
        return;
    }
    
    SearchResultVC *resultVC = [[SearchResultVC alloc] init];
    resultVC.cityName = _cityName;
    resultVC.keyWord = _search_tf.text;
    [self.navigationController pushViewController:resultVC animated:YES];
}

#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self search];
    
    return YES;
}

#pragma mark - 取消搜索
- (void)cancelSearch {
    
    [self .navigationController popViewControllerAnimated:YES];
}

#pragma mark - 展示城市选择
- (void)showAllCity {
    
    _city_data = [[NSUserDefaults standardUserDefaults] objectForKey:USER_CITY_DATA];
    
    if (_city_data && [_city_data count] > 0) {
        
        [self creatCityChooseView];
        
    }else {
        
        // 获取用户ID拥有的地市权限
        [SVProgressHUD showWithStatus:@"加载中..."];
        
        [[ProjectApi instance] getUserCityDataCallBack:^(NetModel *object) {
            
            if (object.isSuccessed) {
                
                self.city_data = [NSArray arrayWithArray:object.service];
                
                [[NSUserDefaults standardUserDefaults] setObject:self.city_data forKey:USER_CITY_DATA];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [SVProgressHUD dismiss];
                
                [self creatCityChooseView];
                
            }else {
                
                [SVProgressHUD showErrorWithStatus:@"地址加载失败"];
                [SVProgressHUD dismissWithDelay:1.5f];
            }
        }];
    }
}

#pragma mark - 创建城市选择视图
- (void)creatCityChooseView {
    
    _cityBaseV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GET_SCREEN_WIDTH(), GET_SCREEN_HEIGHT())];
    _cityBaseV.backgroundColor = [UIColor clearColor];
    [self.view.window addSubview:_cityBaseV];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidCityBaseView)];
    tap.delegate = self;
    [_cityBaseV addGestureRecognizer:tap];
    
    CGFloat table_height = 40*[_city_data count];
    
    if (table_height > GET_SCREEN_HEIGHT()*2/3.0f) {
        
        table_height = GET_SCREEN_HEIGHT()*2/3.0f;
    }
    
    _cityTable = [[CityTableView alloc] initWithFrame:CGRectMake(_cityBtn.left-5, SafeAreaTopHeight, _cityBtn.width+5, table_height) style:UITableViewStylePlain data:_city_data];
    _cityTable.city_delegate = self;
    [_cityBaseV addSubview:_cityTable];
}

#pragma mark - 隐藏城市展示图
- (void)hidCityBaseView {
    
    _cityBaseV.hidden = YES;
}

#pragma mark - 城市选择回调
- (void)cityChooseCallBack:(NSDictionary *)city_dic {
    
    // 隐藏展示图
    [self hidCityBaseView];
    
    // 选择的城市名称
    NSString *chooseCityName = [NSString stringWithFormat:@"%@", city_dic[@"cityname"]];
    
    if ([chooseCityName isEqualToString:_cityName]) {
        return;
    }
    
    _cityName = chooseCityName;
    
    CGFloat cityWidth = [_cityName length]*16;
    
    _cityBtn.frame = CGRectMake(10, SafeAreaTopHeight-7-30, 8+15+5+cityWidth, 30);
    [_cityBtn setTitle:_cityName forState:UIControlStateNormal];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    //判断如果点击的是tableView的cell，就把手势给关闭了
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        
        return NO;
    }
    
    return YES;
}

@end
